package io.awen.modules.sign.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.awen.common.exception.RRException;
import io.awen.common.utils.DateUtils;
import io.awen.common.utils.PageUtils;
import io.awen.common.utils.Query;
import io.awen.modules.sign.StatusConstants;
import io.awen.modules.sign.dao.SignCarLonLatDao;
import io.awen.modules.sign.entity.SignCarEntity;
import io.awen.modules.sign.entity.SignCarLonLatEntity;
import io.awen.modules.sign.entity.SignTaskEntity;
import io.awen.modules.sign.service.SignCarLonLatService;
import io.awen.modules.sign.service.SignCarService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("signCarLonLatService")
public class SignCarLonLatServiceImpl extends ServiceImpl<SignCarLonLatDao, SignCarLonLatEntity> implements SignCarLonLatService {


    @Autowired
    SignCarService signCarService;
    @Autowired
    SignCarLonLatDao signCarLonLatDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SignCarLonLatEntity> page = this.page(
                new Query<SignCarLonLatEntity>().getPage(params),
                new QueryWrapper<SignCarLonLatEntity>()
        );

        return new PageUtils(page);
    }

    /**
     *
     * @param carNumber
     * @return
     */
    @Override
    public SignCarLonLatEntity getLatestByCarNumber(String carNumber) {

        SignCarEntity byCarNum = signCarService.getByCarNum(carNumber);
        if(byCarNum==null){
            throw new RRException("车辆不存在");
        }

        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String date = formatter.format(currentTime);
        SignCarLonLatEntity latestd = signCarLonLatDao.getLatestByCarId(byCarNum.getId(), date);
        return  latestd;

    }
    @Override
    public SignCarLonLatEntity getCarLoc(SignCarEntity car) {

        SignCarLonLatEntity lonLat = signCarLonLatDao.selectOne(new QueryWrapper<SignCarLonLatEntity>()
                .eq("car_id",car.getId())
                .orderByDesc("create_time")
                .last("limit 1")
        );
        return  lonLat;

    }

    @Override
    public List<SignCarLonLatEntity> getListByCarIdAndDate(Long carId, String startDate,String endDate) {
        if(carId==null){
            throw new RRException("车辆Id不存在");
        }
        if(StringUtils.isBlank(startDate)){
            startDate = DateUtils.format(new Date(),DateUtils.DATE_TIME_PATTERN);
            endDate=startDate;
        }

        List<SignCarLonLatEntity> list = signCarLonLatDao.getListByCarId(carId, startDate,endDate);
        return list;
    }


}